package com.yunhe.sms.util;

import com.yunhe.sms.mode.User;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JdbcUtil {
    private static final String URL;
    private static final String USER ;
    private static final String PASSWORD ;
    private static final String DRIVE;
    static {
        File file = new File("src/config.properties");
        Properties properties = new Properties();
        try {
            properties.load(new FileReader(file));
        } catch (IOException e) {
            System.out.println("读取配置文件失败！");
        }
        DRIVE = properties.getProperty("driver");
        URL = properties.getProperty("url");
        USER = properties.getProperty("user");
        PASSWORD = properties.getProperty("password");
        try {
            Class.forName(DRIVE);
        } catch (Exception e) {
            System.out.println("载入驱动失败！");
        }
    }

    /**
     * 增删改
     * @param stmt
     */
    public static void sqlUpdate( PreparedStatement stmt){
        try {
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 查找
     * @param stmt
     * @return
     */
    public static ResultSet sqlQuery( PreparedStatement stmt){
        try {
            return stmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取连接
     * @return
     */
    public static Connection getConn() {
        try {
            return DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            System.out.println("连接数据库失败！");
            return null;
        }
    }

    /**
     * 获取执行器
     * @param conn
     * @param sql
     * @param obj
     * @return
     */
    public static PreparedStatement getstmt(Connection conn,String sql,Object... obj){
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement(sql);
            if (obj !=null){
                for (int i =0; i <obj.length; i++){
                    stmt.setObject(i+1,obj[i]);
                }
            }
            return stmt;
        } catch (SQLException e) {
            System.out.println("获取执行器失败！");
            return null;
        }
    }

    /**
     * 关闭结果集，执行器，连接
     * @param conn
     * @param stmt
     * @param rs
     */
    public static void close(Connection conn,PreparedStatement stmt,ResultSet rs){
        try {
            if (rs!=null){
                rs.close();
            }
            if (stmt!=null){
                stmt.close();
            }
            if (conn!=null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭结果集，执行器
     * @param stmt
     * @param rs
     */
    public static void close(PreparedStatement stmt,ResultSet rs){
        close(null,stmt,rs);
    }

    /**
     * 关闭结果集
     * @param rs
     */
    public static void close(ResultSet rs){
        close(null,null,rs);
    }

    /**
     * 关闭执行器
     * @param stmt
     */
    public static void close(PreparedStatement stmt){
        close(null,stmt,null);
    }

    /**
     * 关闭数据库连接
     * @param conn
     */
    public static void close(Connection conn){
        close(conn,null,null);
    }

    public static void main(String[] args) {
        Connection conn = JdbcUtil.getConn();
       /* String sqlQuery = " select *from user where id = ?";
        PreparedStatement stmtQuery = JdbcUtil.getstmt(conn, sqlQuery, 1);
        ResultSet resultSet = JdbcUtil.sqlQuery(stmtQuery);//调用查找方法，获得查找结果集
        try {
            while (resultSet.next()){
                System.out.println(resultSet.getString("user_name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtil.close(stmtQuery,resultSet);//关闭结果集，执行器*/
        String dle ="delete from user where id = ?";
        PreparedStatement stmt = JdbcUtil.getstmt(conn, dle, 3);
        JdbcUtil.sqlUpdate(stmt);
        JdbcUtil.close(conn);//关闭数据库连接
    }

}
